//
//  MeFillAddressViewController.m
//  RainbowFM
//
//  Created by jv on 14-12-10.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "MeFillAddressViewController.h"
#import "AreaTableViewController.h"
#import "UserModel.h"

@interface MeFillAddressViewController ()<AreaTableViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *addressField;
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;

@property (nonatomic,strong) NSDictionary *addrDict;

@end

@implementation MeFillAddressViewController


-(void)fetchData{
    [[UserModel sharedModel] fetchUserAddressDetailWithDeliID:_aId result:^(BOOL success, NSDictionary *dict) {
        _addrDict = [NSDictionary dictionaryWithDictionary:dict];
        [self initViewData];
    }];
}

-(void)initViewData{
    self.nameField.text = _addrDict[@"deliName"];
    self.phoneField.text = _addrDict[@"deliTel"];
    self.areaLabel.text = _addrDict[@"deliAreaAddr"];
    self.addressField.text = _addrDict[@"deliAddr"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _fillAddressView.layer.borderColor = [UIColor colorWithRed:0.803922F green:0.803922F blue:0.803922F alpha:1.0F].CGColor;
    _fillAddressView.layer.borderWidth = 1.f;

    if (_addressType == 1) {
        [self fetchData];
    }
    
}


-(void)didAreaSelectedWithCtrl:(AreaTableViewController *)ctrl areaName:(NSString *)areaName{
    self.areaLabel.text = areaName;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        [self.nameField resignFirstResponder];
        [self.addressField resignFirstResponder];
        [self.phoneField resignFirstResponder];
        AreaTableViewController *ctrl = [AreaTableViewController new];
        ctrl.areaType = 1;
        ctrl.delegate = self;
        [self.navigationController pushViewController:ctrl animated:YES];
    }
}




- (IBAction)submitAction:(id)sender {
    if (_addressType == 0) {
        [[UserModel sharedModel] addAddressWithDeliName:_nameField.text deliTel:_phoneField.text deliAreaAddr:_areaLabel.text deliDtlAddr:_addressField.text result:^(BOOL success) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"添加成功!"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
    else{
        [[UserModel sharedModel] editAddressWithDeliName:_nameField.text deliID:_aId deliTel:_phoneField.text deliAreaAddr:_areaLabel.text deliDtlAddr:_addressField.text result:^(BOOL success) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"修改成功!"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
